package controllers

import config.{Global, MyAwait, MyFile, MyJson}
import dao.GenomeDao
import play.api.data.Form
import play.api.data.Forms._
import play.api.libs.json.{JsObject, Json}
import play.api.mvc.{AbstractController, ControllerComponents}
import utils.TableUtils

import javax.inject.Inject
import scala.concurrent.ExecutionContext

class VariationController @Inject()(genomeDao: GenomeDao, cc: ControllerComponents)
                                   (implicit exec: ExecutionContext) extends AbstractController(cc) with MyAwait with MyFile with MyJson{


  case class VarData(ids: String)

  val VarForm = Form(
    mapping(
      "ids" -> text
    )(VarData.apply)(VarData.unapply)
  )

  case class VarRangeData(chr: String, start: Int, end: Int)

  val VarRangeForm = Form(
    mapping(
      "chr" -> text,
      "start" -> number,
      "end" -> number
    )(VarRangeData.apply)(VarRangeData.unapply)
  )


  def getGeneVarData(formId: String) = Action { implicit request =>

    val (path, path1) = if (Global.isWindow) {
      ("H:\\紫花苜蓿数据库\\2023_9_22\\网站/gene", "H:\\紫花苜蓿数据库\\gff\\range")
    } else {
      (s"${Global.path}/config/variable", s"${Global.path}/config/range")
    }

    val geneids = formId match {
      case "geneIdForm" =>
        VarForm.bindFromRequest.get.ids.trim.split(",").map(_.trim).toSeq
      case "rangeForm" =>
        val data = VarRangeForm.bindFromRequest.get
        genomeDao.getByRange("ZMNO", data.chr, data.start, data.end).toAwait.map(_.geneid)
    }



   val snpPlot =  geneids.map { geneid =>
      val file = s"$path/$geneid.txt".toFile
     val info = genomeDao.getById(TableUtils.geneIdToId(geneid)).toAwait

      if (!file.exists()) {
        (Json.obj("valid" -> "false"),
          Json.obj("id" -> info.id, "geneid" -> info.geneid, "chr" -> info.chr, "start" -> info.start,
            "end" -> info.end, "strand" -> info.strand, "snp" -> 0, "indel" -> 0, "sv" ->0))
      } else {
        val len = info.end - info.start + 1

        val lines = file.readLines

        val snp = lines.filter(_.startsWith("SNP")).map(_.split("\t").tail.zipWithIndex.map { y => y._2.toString -> y._1 }.toMap)
        val indel = lines.filter(_.startsWith("Indel")).map(_.split("\t").tail.zipWithIndex.map { y => y._2.toString -> y._1 }.toMap)
        val sv = lines.filter(_.startsWith("SV")).map(_.split("\t").tail.zipWithIndex.map { y => y._2.toString -> y._1 }.toMap)


        val pie = Seq(snp.length,indel.length,sv.length)

        val table = Json.obj("id" -> info.id,"geneid" -> info.geneid,"chr" -> info.chr,"start" -> info.start,
        "end" -> info.end,"strand"->info.strand,"snp" -> snp.length,"indel" ->indel.length,"sv" -> sv.length)

        val check = s"$path1/$geneid.txt".readFileToString

        val scatter = s"${path}3/$geneid.txt".readFileToString
        (Json.obj("snpData" -> snp, "indelData" -> indel, "svData" -> sv, "scatter" -> scatter , "strand" -> info.strand, "check" -> check ,
          "len" -> len,"name" -> geneid,"pie" -> pie),table)
      }
    }

    Ok(Json.obj("snpPlot" -> snpPlot.map(_._1),"table" ->snpPlot.map(_._2)))

  }
}
